package com.ambimmort.ucagent.modules.webservice;


import cn.net.greenet.db.HibernateUtil;
import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hedingwei on 2015/1/15.
 */
public class Worker implements Runnable {

    private List<Thing> msgs = null;

    public List<Thing> getMsgs() {
        return msgs;
    }

    public void setMsgs(List<Thing> msgs) {
        this.msgs = msgs;
    }

    public void run() {
        System.out.println("Start Processing Cache[" + this + "]: " + msgs.size());
        Session s = HibernateUtil.getSessionFactory().openSession();
        s.setCacheMode(CacheMode.IGNORE);
        Transaction tx = null;
        try {


            List<Thing> addThings = new ArrayList<Thing>();
            List<Thing> deleteThings = new ArrayList<Thing>();
            List<Thing> updateThings = new ArrayList<Thing>();
            for (Thing t : msgs) {
                if (t != null) {
                    if (t.getAction() == Thing.ADD) {
                        addThings.add(t);
                    } else if (t.getAction() == Thing.DLETE) {
                        deleteThings.add(t);
                    } else if (t.getAction() == Thing.UPDATE) {
                        updateThings.add(t);
                    }
                }
            }

            int i = 0;
            if (!addThings.isEmpty()) {
                tx = s.beginTransaction();
                for (Thing t : addThings) {
                    i++;
                    if (t != null) {
                        if (t.getAction() == Thing.ADD) {
                            s.save(t.getData());

                        }
                        if (i % 50 == 0) {
                            s.flush();
                            s.clear();
                        }
                    }
                }
                tx.commit();
                System.out.println("add " + i + " records");
            }

            i = 0;
            if (!deleteThings.isEmpty()) {
                tx = s.beginTransaction();
                for (Thing t : deleteThings) {
                    i++;
                    if (t != null) {
                        if (t.getAction() == Thing.DLETE) {
                            s.delete(t.getData());

                        }
                        if (i % 50 == 0) {
                            s.flush();
                            s.clear();
                        }
                    }
                }
                tx.commit();
                System.out.println("delete " + i + " records");
            }

            if (!updateThings.isEmpty()) {
                tx = s.beginTransaction();
                for (Thing t : updateThings) {
                    i++;
                    if (t != null) {
                        if (t.getAction() == Thing.UPDATE) {
                            s.update(t.getData());

                        }
                        if (i % 50 == 0) {
                            s.flush();
                            s.clear();
                        }
                    }
                }
                tx.commit();
                System.out.println("update " + i + " records");
            }


            System.out.println("Processing Cache Finished[" + this + "]: " + msgs.size());
        } catch (Throwable e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        } finally {
            if (s != null) {
                s.close();
            }
        }


    }
}

